package str;

public class XC {
    public static void main(String[] args) {
        X1 x1 = new X1();
        Thread t = new Thread(()->{
           char c;
           for(int i=0 ; i<10 ;i++){
               c = (char) (Math.random()*26+'A');
               x1.push(c);
               System.out.println("打印"+c);
               try {
                   Thread.sleep((int)(Math.random()*1000));
               }catch (Throwable r){
                   r.printStackTrace();
               }
           }
        });
        Thread t1 = new Thread(()->{
            char c;
            for (int i = 0 ; i<10;i++){
                c = x1.pop();
                System.out.println("取出"+c);
                    try {
                        Thread.sleep((int)(Math.random()*1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
        });
        t.start();
        t1.start();
    }
}
class X1{
    private int i = 0;
    private char[] data= new char[5];
    public synchronized void push(char c){
        while ((i==data.length)){
            try{
                this.wait();
            }catch (Throwable e){
                e.printStackTrace();
            }}
            this.notify();
            data[i]=c;
            i++;

    }
    public synchronized char pop(){
        while (i==0){
            try {
                this.wait();
            }catch (Throwable ignored){
            }
        }
            this.notify();
            i--;
        return data[i];
    }
}